using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Shuriken.Core;
using Shuriken.Core.Store;
using Shuriken.Core.Utils;

namespace Shuriken
{
    public partial class frmSettings : Form
    {
        private Keys m_Hotkey;
         
        public frmSettings()
        {
            InitializeComponent();
            this.Text = string.Format("{0} Settings", Shuriken.Core.ApplicationEnvironment.ProductNameShort);
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            ListViewGroup lvGroup;
            Dictionary<string, ListViewGroup> groupDict = new Dictionary<string, ListViewGroup>();


            // Reading Settings for Stores 
            listViewStore.Items.Clear();
            listViewStore.Groups.Clear();

            foreach (IStore store in Core.Store.StoreManager.GetDefaultInstance().StoreList)
            {
                if (groupDict.ContainsKey(store.StoreCategory))
                {
                    lvGroup = groupDict[store.StoreCategory];
                }
                else
                {
                    lvGroup = new ListViewGroup(store.StoreCategory, store.StoreCategory);
                    groupDict.Add(store.StoreCategory, lvGroup);
                    listViewStore.Groups.Add(lvGroup);
                }

                string status = store.StoreEnabled ? "Enabled" : "Disabled";
                ListViewItem listViewItem = new System.Windows.Forms.ListViewItem(new string[] {
                    store.StoreName,
                    store.Count.ToString(),
                    status}, -1);

                listViewItem.Tag = store.StoreEnabled; 
                listViewItem.Group = lvGroup;
                listViewStore.Items.Add(listViewItem);
            }

            // Querying Hotkey
            hotkeyEditor1.Shortcut = ApplicationEnvironment.Preferences.HotKey; 

            // Releasing hotkey registration for now.
            Hotkey.ReleaseHotkeyTemporarly(); 

            // Querying Application AutoStart 
            checkAutoStart.Checked = ApplicationEnvironment.Preferences.AutoStart; 
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool result; 
            // Saving the store status changes
            List<IStore> storeList = Core.Store.StoreManager.GetDefaultInstance().StoreList;

            for (int loop = 0; loop < storeList.Count; loop++)
            {
                IStore store = storeList[loop];
                store.StoreEnabled = (bool)listViewStore.Items[loop].Tag; 
            }

            // Checking for Hotkey change 
            if (!ApplicationEnvironment.Preferences.HotKey.Equals(hotkeyEditor1.Shortcut))
            {
                m_Hotkey = ApplicationEnvironment.Preferences.HotKey;               

                // Hotkey is dirty, so it needs to be saved to settings and to reregister!
                ApplicationEnvironment.Preferences.HotKey = hotkeyEditor1.Shortcut;

                try
                {
                    result = Hotkey.RegisterFormHotkey(hotkeyEditor1.Shortcut);
                }
                catch (Exception excp)
                {
                    // Failed to register hotkey, now try to fall back 
                    // to the previous hotkey.
                    MessageBox.Show("There were problems registering the changes you made. " + excp.Message + "!");

                    // Reverting to and reregistering the old hotkey. 
                    ApplicationEnvironment.Preferences.HotKey = m_Hotkey;
                    Hotkey.AcquireHotkeyAgain();
                }
            }
            else
            {
                // Registering the old hotkey
                Hotkey.AcquireHotkeyAgain();
            }

            // Autostart 
            if (checkAutoStart.Checked != ApplicationEnvironment.Preferences.AutoStart)
            {
                result = false; 
                if (checkAutoStart.Checked)
                {
                    result = AutoStart.EnableAutoStart();
                }
                else
                {
                    result = AutoStart.DisableAutoStart();
                }

                if (result)
                {
                    ApplicationEnvironment.Preferences.AutoStart = checkAutoStart.Checked;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // Not doing anything now 
            Hotkey.AcquireHotkeyAgain();
        }

        private void listViewStore_DoubleClick(object sender, EventArgs e)
        {
            // check the selected item, only one item can be selected
            if (listViewStore.SelectedIndices.Count == 1)            
            {
                int index = listViewStore.SelectedIndices[0]; 
                ListViewItem lvi = listViewStore.Items[index];
                bool enabled = !((bool)listViewStore.Items[index].Tag);
                lvi.Tag = enabled; 
                lvi.SubItems[2].Text = enabled ? "Enabled" : "Disabled";                                 
            }
        }
    }
}